package com.hjx.face.javacv.face;

import java.awt.EventQueue;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;


import java.awt.BorderLayout;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.border.LineBorder;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.event.ActionEvent;

public class FaceRegisterApp {

	private JFrame frame;
	private JTextField txt_registerName;
	JavacvService javacvService=new JavacvService();
	private JPanel p_play;
	private JButton btn_photo;
	private JButton btn_save;
	
	BufferedImage[] faces=new BufferedImage[5];
	private JLabel lbl_face1;
	private JLabel lbl_face2;
	private JLabel lbl_face3;
	private JLabel lbl_face4;
	private JLabel lbl_face5;
	
	private JLabel lbl_selectedLabel;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
					FaceRegisterApp window = new FaceRegisterApp();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FaceRegisterApp() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 668, 691);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				javacvService.stop();
			}
		});
		
		txt_registerName = new JTextField();
		panel.add(txt_registerName);
		txt_registerName.setColumns(10);
		
		JButton btn_register = new JButton("注册");
		btn_register.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String text = txt_registerName.getText();
				if (text==null||text.trim().equals("")) {
					JOptionPane.showMessageDialog(frame, "请输入名称");
					return;
				}
				Point imageSize = javacvService.getImageSize();
				System.out.println(imageSize);
				boolean play = javacvService.play(p_play);
				if (!play) {
					JOptionPane.showMessageDialog(frame, "视频正在启动请等待。。。");
					return;
				}
				btn_register.setEnabled(false);
				btn_photo.setEnabled(true);
				btn_save.setEnabled(true);
				txt_registerName.setEditable(false);
			}
		});
		panel.add(btn_register);
		
		btn_photo = new JButton("拍照");
		btn_photo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BufferedImage image = javacvService.getFaceImage();
				Graphics g = lbl_selectedLabel.getGraphics();
				Rectangle bounds = lbl_selectedLabel.getBounds();
				g.drawImage(image, 0, 0, bounds.width, bounds.height, lbl_selectedLabel);
				try {
					String name = lbl_selectedLabel.getName();
					name= name.substring(name.length()-1);
					faces[Integer.valueOf(name)-1]=image;
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});
		btn_photo.setEnabled(false);
		panel.add(btn_photo);
		
		btn_save = new JButton("保存");
		btn_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					BufferedImage image = javacvService.getFactImage();
					JavacvService.saveImage(image,"testImage.jpg");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				
				for (BufferedImage bufferedImage : faces) {
					if (bufferedImage==null) {
						JOptionPane.showMessageDialog(frame, "请注册好五个头像");
						return;
					}
				}
				javacvService.registerFace(txt_registerName.getText(), faces);
				btn_register.setEnabled(!false);
				btn_photo.setEnabled(!true);
				btn_save.setEnabled(!true);
				txt_registerName.setEditable(!false);
			}
		});
		btn_save.setEnabled(false);
		panel.add(btn_save);
		
		JPanel panel_2 = new JPanel();
		frame.getContentPane().add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(null);
		
		p_play = new JPanel();
		p_play.setBounds(10, 10, 640, 480);
		panel_2.add(p_play);
		
		lbl_face2 = new JLabel("");
		lbl_face2.setBorder(new LineBorder(new Color(0, 0, 0)));
		lbl_face2.setBounds(109, 517, 89, 90);
		panel_2.add(lbl_face2);
		
		lbl_face3 = new JLabel("");
		lbl_face3.setBorder(new LineBorder(new Color(0, 0, 0)));
		lbl_face3.setBounds(208, 517, 89, 90);
		panel_2.add(lbl_face3);
		
		lbl_face4 = new JLabel("");
		lbl_face4.setBorder(new LineBorder(new Color(0, 0, 0)));
		lbl_face4.setBounds(307, 517, 89, 90);
		panel_2.add(lbl_face4);
		
		lbl_face5 = new JLabel("");
		lbl_face5.setBorder(new LineBorder(new Color(0, 0, 0)));
		lbl_face5.setBounds(406, 517, 89, 90);
		panel_2.add(lbl_face5);
		
		lbl_face1 = new JLabel("");
		lbl_face1.setBorder(new LineBorder(new Color(0, 0, 0)));
		lbl_face1.setBounds(10, 517, 89, 90);
		lbl_face1.setName("face1");
		lbl_face2.setName("face2");
		lbl_face3.setName("face3");
		lbl_face4.setName("face4");
		lbl_face5.setName("face5");
		panel_2.add(lbl_face1);
		addfaceListener(panel_2);
//		javacvService.setClassiferName(null);
		javacvService.start();
		lbl_selectedLabel=lbl_face1;
	}

	private void addfaceListener(JPanel panel_2) {
		Component[] components = panel_2.getComponents();
		for (Component component : components) {
			if (component instanceof JLabel) {
				component.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						lbl_selectedLabel=(JLabel) component;
						
					}
				});
			}
		}
	}

	public void open(int closeStyle) {
		frame.setVisible(true);
		frame.setDefaultCloseOperation(closeStyle);
	}

	public JFrame getFrame() {
		return frame;
	}
}
